<?php
/**
 * @filesource:					RoleAction.class.php
 * @desc:						角色管理控制器类
 * 
 * @author:						houyining
 * @copyright:					hou.yining@gmail.com
 * @date:						2011-04-09
 */
class RoleAction extends CommonAction {
	public function index(){
		$crumb_info = array(
			array('uri' => '__URL__', 'title' => L('role_manage'), 'sort' => C('CRUMB_LAST')),
		);
		
		$this->_mergeCrumbInfo($crumb_info);
		$this->_action_link = array(
			array('uri' => '__URL__/add', 'title' => L('add_role')),
			);
		$this->assign('crumb_info', $this->_crumb_info);
		$this->assign('action_link', $this->_action_link);
		parent::index();
	}
	
	public function add(){
		$crumb_info = array(
			array('uri' => '__URL__', 'title' => L('role_manage'), 'sort' => C('CRUMB_DEFAULT')),
			array('uri' => '__URL__/add', 'title' => L('add_role'), 'sort' => C('CRUMB_LAST')),
		);
		
		$this->_mergeCrumbInfo($crumb_info);
		$this->_action_link = array(
			array('uri' => '__URL__/index', 'title' => L('list_role')),
			);
		
		$this->assign('crumb_info', $this->_crumb_info);
		$this->assign('action_link', $this->_action_link);
		parent::add();
	}
	
	public function edit(){
		$crumb_info = array(
			array('uri' => '__URL__', 'title' => L('role_manage'), 'sort' => C('CRUMB_DEFAULT')),
			array('uri' => '__URL__/edit/id/' . $_REQUEST['id'], 'title' => L('edit_role'), 'sort' => C('CRUMB_LAST')),
		);

		$this->_mergeCrumbInfo($crumb_info);
		$this->_action_link = array(
			array('uri' => '__URL__/index', 'title' => L('list_role')),
			);
		
		$this->assign('crumb_info', $this->_crumb_info);
		$this->assign('action_link', $this->_action_link);
		parent::edit();
	}
	
	//查看当前角色下的所有用户
	public function showUsers(){
		$role_id = $_REQUEST['role_id'];
		
		$crumb_info = array(
			array('uri' => '__URL__', 'title' => L('role_manage'), 'sort' => C('CRUMB_DEFAULT')),
			array('uri' => '__URL__/showUsers/role_id/' . $role_id, 'title' => L('user_list'), 'sort' => C('CRUMB_LAST')),
		);
		
		$this->_mergeCrumbInfo($crumb_info);
		
		$this->assign('crumb_info', $this->_crumb_info);
		
		//得到当前角色组下的用户列表
		$role = new RoleModel();
		$user_role_list_v = $role->getUserList($role_id);
		
		$user_role_list = array();
		foreach($user_role_list_v as $v){
			$user_role_list[$v['id']] = $v['id'];
		}

		//得到所有用户列表
		$admin_user = new AdminUserModel();
		$user_list = $admin_user->field("id,account")->select();
		
		$this->assign('user_role_list', $user_role_list);
		$this->assign('role_id', $role_id);
		$this->assign('user_list', $user_list);
		$this->display();
	}
	
	public function addUsers(){
		$role_users_id = $_REQUEST['role_users_id'];
		$role_id = $_REQUEST['role_id'];

		$role_user = new RoleUserModel();
		$role_user->deleteRoleUser($role_id);
		
		$data = array('role_id' => $role_id,
					  '__hash__' => $_REQUEST['__hash__']);		
		foreach($role_users_id as $id){
			$data['user_id'] = $id;
			
			if(false === $role_user->create($data)){
				$this->_assign_fail();
			}
			if(false === $role_user->add()){
				$this->_assign_fail();
			}
		}
		$this->_assign_success();
	}
	
	public function showPrivilege(){
		$role_id = $_REQUEST['role_id'];
		
		$crumb_info = array(
			array('uri' => '__URL__', 'title' => L('role_manage'), 'sort' => C('CRUMB_DEFAULT')),
			array('uri' => '__URL__/showPrivilege/role_id/' . $role_id, 'title' => L('set_privilege'), 'sort' => C('CRUMB_LAST')),
		);
		
		$this->_mergeCrumbInfo($crumb_info);		
		$this->assign('crumb_info', $this->_crumb_info);
		
		//得到当前角色的所有权限
		$access = new AccessModel();
		$role_access_list_v = $access->where(array('role_id'=>$role_id))->select(); 
		
		$role_access_list = array();
		foreach($role_access_list_v as $role_access){
			$role_access_list[$role_access['node_id']] = $role_access['node_id'];
		}
		
		$node = new NodeModel();
		$nodes = $node->getNodeTree();

		$this->assign('role_access_list', $role_access_list);
		$this->assign('role_id', $role_id);
		$this->assign('nodes', $nodes);
		$this->display();
	}
	
	public function setPrivileges(){
		$role_id = $_REQUEST['role_id'];		
		$nodes_id = $_REQUEST['nodes_id'];
		
		$access = new AccessModel();
		$access->deleteByRoleId($role_id);
		
		$data = array('role_id' => $role_id,
					  '__hash__' => $_REQUEST['__hash__']);	
		
		$node = new NodeModel();
		$nodes_id[] = $node->getRootNodeId();
		
		foreach($nodes_id as $node_id){
			$data['node_id'] = $node_id;
			
			if(false === $access->create($data)){
				$this->_assign_fail();
			}
			if(false === $access->add()){
				$this->_assign_fail();
			}
		}
		$this->_assign_success();
	}
}
?>